import { _decorator, Component, UIOpacity, tween, Tween, Vec3 } from 'cc';

const { ccclass, property } = _decorator;

@ccclass('AutoBlink')
export class AutoBlink extends Component {
    @property
    interval: number = 1;

    private _uiOpacity: UIOpacity | null = null;
    private _blinkTween: Tween<UIOpacity> | null = null;

    onLoad() {
        this._uiOpacity = this.getComponent(UIOpacity);
        if (!this._uiOpacity) {
            console.warn('AutoBlink: Missing UIOpacity component.');
            return;
        }

        this._createBlinkTween();
    }

    private _createBlinkTween() {
        if (!this._uiOpacity) return;

        this._blinkTween = tween(this._uiOpacity)
            .to(this.interval / 2, { opacity: 0 })
            .to(this.interval / 2, { opacity: 255 })
            .union()
            .repeatForever();

        this._blinkTween.start();
    }

    onDestroy() {
        if (this._blinkTween) {
            this._blinkTween.stop();
            this._blinkTween = null;
        }
    }
}